package com.springboot.demo.utils;

import com.springboot.demo.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

/**
 * @author wubc683
 * @desc
 * @date 2021-08-23 9:14
 */
//@Component
@Slf4j
public class FileUtils {
    public static void  upload(MultipartFile multipartFile, String path) throws Exception {
        if (multipartFile != null) {
            File file = new File(path);
            if (!file.exists()) {//如果文件夹不存在
                file.mkdirs();//创建文件夹
            }
            try {
                multipartFile.transferTo(file);
            } catch (IOException e) {
                log.error("file upload failed");
                throw new Exception();
            }
        }
    }
//        if (files.length > 0) {
//            for (MultipartFile file : files) {
//                if (!file.isEmpty()) {
//                    String originalFilename = file.getOriginalFilename();
//                    File f = new File(path + "\\" + originalFilename);
//                    if (!f.exists()) {//如果文件夹不存在
//                        f.mkdirs();//创建文件夹
//                    } else {
//                        log.error("profile upload failed,id={},username={}", user.getId(), user.getUsername());
//                        throw new BusinessException(ResponseResult.FILE_EXISTED);
//                    }
//                    try {
//                        file.transferTo(f);
//                    } catch (IOException e) {
//                        log.error("file upload failed");
//                        throw new Exception();
//                    }
//                }
//            }
//        }
//    }

}
